$().ready(function () {
	$('.summernote').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFile(files);
			}
		}
	});

	$('.brandCharacteristics').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFile(files);
			}
		}
	});

});





$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

//上传品牌照片
var arrImg = [];
layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		multiple:true,
		number:5,
		size:10240,
		// before:function (r){
		// 	if( arrImg.length <= 5 ){
		//
		// 	}else{
		// 		layer.confirm("图片最多只能上传5张", {
		// 			btn: ['确定', '取消']
		// 		})
		// 		return false;
		// 	}
		// },
		done: function (r) {
			var str = '';
			arrImg.push(r.data);
			str = arrImg.join();
			$("#brandImg").val(str);
			str = '';
			arrImg.forEach(function (item,index){
				str += `<div class="closeBtn"><img class="upImg" src="${item}" /><span class="iconclose"><img src="../../img/close.png"></span></div>`
			})
			$("#brandImgs").html(str)

			$(".iconclose").click(function (){
				$(this).parent('.closeBtn').remove();
			})

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


//上传品牌logo
layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnlogoAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		size:10240,
		done: function (r) {
			$("#brandlogo").val(r.data);
			$("#brandlogos").attr("src",r.data)

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});



function save() {
	$("#brandSchoolEnterprise").val($('.summernote').summernote('code'));
	$("#brandCharacteristics").val($('.brandCharacteristics').summernote('code'));
	if( arrImg.length <= 0 ){
		layer.msg('请上传品牌图片');
		return false;
	}
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/sysBrand/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}